package com.sharding.transaction.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

public class OrderTableShardingAlgorithm implements PreciseShardingAlgorithm<Date> {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {

		//preciseShardingValue就是当前插入的字段值
        //collection 内就是所有的逻辑表
        //获取字段值
//        LocalDateTime time = preciseShardingValue.getValue();
//        return "orders_" + (time.getMonth().getValue() - 1);
        SimpleDateFormat format = new SimpleDateFormat("MM");
        return "orders_" + (Integer.valueOf(format.format(preciseShardingValue.getValue())) - 1);
    }

}
